package onion.molingo2o.activity.ablumimage;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.easemob.chatuidemo.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import onion.molingo2o.Config;
import onion.molingo2o.R;
import onion.molingo2o.utils.ImageModel;
import onion.molingo2o.utils.imageutil.AlbumHelper;
import onion.molingo2o.utils.imageutil.BitmapHelper;


public class AlbumDetailActivity extends BaseActivity {
    /**
     * logger
     */
    /**
     * tag to get iamge list
     */

    /**
     * result code fo album detail
     */
    public static final int resultCode_to_album = 1;

    /**
     * image list
     */
    private ArrayList<ImageModel> imageModelList = new ArrayList<>();
    /**
     *
     */
    public static ArrayList<String> pathList=new ArrayList<>();
    /**
     * gridview for album
     */
    private GridView gv_album_detail;
    /**
     * adapter for album
     */
    private AlbumDetailGridAdapter albumDetailGridAdapter;
    /**
     * album helper class
     */
    private AlbumHelper helper;
    /**
     * button for image selected
     */
    private Button btn_image_selected;
    private int imageCount;
    public static final int PREVIEW = 2015929;
    public static final int REQUEST_SEEBIGIMG = 617378;
    /**
     * handler
     */
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Toast.makeText(AlbumDetailActivity.this, "最多选择" + imageCount + "张图片", Toast.LENGTH_SHORT).show();
//				Toast.makeText(AlbumDetailActivity.this, "最多选择6张图片", Toast.LENGTH_SHORT).show();
                    break;

                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album_detail);
        initTitle(null, "相册", "");
        imageCount = getIntent().getIntExtra("ImageCount", 6);
        //get helper
        helper = AlbumHelper.getHelper();
        helper.init(getApplicationContext());
        //get image model list
        imageModelList.addAll((List<ImageModel>) getIntent().getSerializableExtra(
                Config.EXTRA_IMAGE_LIST));
        for (int i = 0; i < imageModelList.size(); i++) {
            if (BitmapHelper.imageListStorage.contains(imageModelList.get(i).imagePath))
                imageModelList.get(i).isSelected = true;
        }
//        addActionMenuItem(PREVIEW, R.drawable.preview);
        initView();
    }
    private void changeComplete(int count){
        if(count>0){
            btn_image_selected.setSelected(true);
            btn_image_selected.setClickable(true);
            btn_image_selected.setText("完成" + "(" + count + ")");
        }else {
            btn_image_selected.setSelected(false);
            btn_image_selected.setClickable(false);
            btn_image_selected.setText("完成");
        }

    }


    /**
     * init view
     */
    private void initView() {
        gv_album_detail = (GridView) findViewById(R.id.gv_album_detail);
        gv_album_detail.setSelector(new ColorDrawable(Color.TRANSPARENT));
        albumDetailGridAdapter = new AlbumDetailGridAdapter(AlbumDetailActivity.this, imageCount, imageModelList,
                mHandler);
        albumDetailGridAdapter.setSelectTotal(BitmapHelper.imageCount);
        gv_album_detail.setAdapter(albumDetailGridAdapter);
        albumDetailGridAdapter.setTextCallback(new AlbumDetailGridAdapter.TextCallback() {
            public void onListen(int count) {
                changeComplete(count);
            }
        });

        gv_album_detail.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                //albumDetailGridAdapter.notifyDataSetChanged();
                Intent intent = new Intent(AlbumDetailActivity.this, ShowAllImgActivtiy.class);
                intent.putExtra(Config.EXTRA_IMAGE_LIST, imageModelList);
                intent.putExtra(Config.INTENT_PARMAS1, albumDetailGridAdapter.getSelectTotal());
                intent.putExtra("position", position);
                startActivityForResult(intent, REQUEST_SEEBIGIMG);
            }

        });

        btn_image_selected = (Button) findViewById(R.id.btn_image_selected);
        changeComplete(BitmapHelper.imageListStorage.size() );
        btn_image_selected.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

//                if (BitmapHelper.act_bool) {
//                    logger.info("go to newActivity ");
//                    logger.info("act_boll is :"+BitmapHelper.act_bool);
//                    Intent intent = new Intent(AlbumDetailActivity.this,NewActivityActivity.class);
//                    startActivity(intent);
//                    BitmapHelper.act_bool = false;
//                }
                Intent intentPop = new Intent();
                intentPop.setClass(AlbumDetailActivity.this, AlbumActivity.class);
                if (getIntent().getIntExtra(Config.INTENT_PARMAS1, 0) == Config.SelectAblum) {
//                    BitmapHelper.bitmapChatListStorage=list;
                    setResult(RESULT_OK, intentPop);
                } else {
                    BitmapHelper.addAll(pathList,imageCount);
//                    Intent intent = new Intent();
//                    intent.setClass(AlbumDetailActivity.this,NewActivityActivity.class);
//                    startActivity(intent);
                    setResult(resultCode_to_album, intentPop);
                }
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    pathList.clear();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SEEBIGIMG) {
            if (resultCode == RESULT_OK) {//预览页面点击了完成
                Intent intentPop = new Intent();
//                if (getIntent().getIntExtra(Config.INTENT_PARAMS1, 0) == Config.SelectAblum) {
////                    BitmapHelper.bitmapChatListStorage=list;
//                    setResult(RESULT_OK, intentPop);
//                } else {
                    BitmapHelper.addAll(pathList,imageCount);
                    setResult(resultCode_to_album, intentPop);
//                }
                finish();
            } else if(data!=null){//预览页面点击了返回
                List<ImageModel>  list= (List) data.getSerializableExtra(Config.EXTRA_IMAGE_LIST);
              if(list!=null) {
                  imageModelList.clear();
                  imageModelList.addAll(list);
                  albumDetailGridAdapter.setSelectTotal(data.getIntExtra(Config.INTENT_PARMAS1, 0));
                  changeComplete(data.getIntExtra(Config.INTENT_PARMAS1, 0));
                  albumDetailGridAdapter.notifyDataSetChanged();

              }
            }
        }
    }
}
